//
//  oldMomentViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/4/2.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldMomentViewController.h"
#import "oldMomentsTableViewCell.h"
#import "oldMomentDetalViewController.h"
#import "oldMomentPublishViewController.h"
#import "oldMessageView.h"
#import "oldMessageListViewController.h"

@interface oldMomentViewController ()<oldMessageViewDelegate>
@property (nonatomic, copy)NSArray *dataArray;
@property (nonatomic, strong)oldMessageView *messageView;
@end

static NSString *const identifier = @"oldMomentsTableViewCell";

@implementation oldMomentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_messageView addToView];
    [self.tableView.mj_header beginRefreshing];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_messageView removeView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    [self rigisterCell:identifier];
    self.tableView.estimatedRowHeight = 213;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    UIButton *rightbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    
    rightbutton.titleLabel.font = [UIFont systemFontOfSize:15];
    //[leftbutton setBackgroundColor:[UIColor blackColor]];
    
    [rightbutton setTitle:@"发表" forState:UIControlStateNormal];
    
    [rightbutton addTarget:self action:@selector(rightbtnTapped:) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:rightbutton];
    
    self.navigationItem.rightBarButtonItem = rightitem;
    
    _messageView = [[oldMessageView alloc] init];
    _messageView.delegate = self;
    if (![_messageView superview]) {
        [_messageView addToView];
    }
}

- (void)rightbtnTapped:(id)sender
{
    oldMomentPublishViewController *vc = [oldMomentPublishViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)messageViewTapped
{
    oldMessageListViewController *vc = [oldMessageListViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
//    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"pageNum", @"10", @"pageSize", nil];
    
    [self loadData:params ifResetPageNo:YES];
}

- (void)loadMore
{
    NSString *pageNum = [NSString stringWithFormat:@"%ld", (long)pageNo + 1];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:pageNum, @"pageNum", @"10", @"pageSize", nil];
    
    [self loadData:params ifResetPageNo:NO];
}

- (void)loadData:(NSDictionary *)params ifResetPageNo:(BOOL)reset
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_GetAllFeedList params:params success:^(id data) {
        if (reset) {
            pageNo = 1;
            weakSelf.dataArray = [NSArray array];
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            pageNo ++;
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
        
    } error:^(NSString *error) {
        if (reset) {
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [oldHub warningUser:error];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [_dataArray mutableCopy];
    
    for (int i = 0; i < [data count]; i++) {
        oldMoment *moment = [[oldMoment alloc] initWithDictionary:data[i]];
        [array addObject:moment];
    }
    
    _dataArray = array;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldMomentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldMomentsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.moment = _dataArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldMomentDetalViewController *vc = [oldMomentDetalViewController new];
    vc.moment = _dataArray[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
